package ay.shadow.order.controller;

import ay.shadow.order.entity.Order;
import ay.shadow.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 *
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("order")
@RefreshScope
public class OrderController {
    private final StockFeignClient stockFeignClient;
    private final DynamicStockFeignClient dynamicStockFeignClient;
    private final RestTemplate restTemplate;
    private final OrderService orderService;
    @Value("${spring.autoher}")
    private String author;

    @GetMapping("getOrder")
    public String getOrder() {
        // 使用OpenFeign跨命名空间访问stock服务
//        restTemplate.getForObject("http://shadow-stock.stock-server/stock/getStock", String.class);
        String result = stockFeignClient.getStock();
        System.out.println(author+"OpenFeign跨命名空间访问结果: " + result);
        return result;
    }

    @GetMapping("getOrderMethod5")
    public String getOrderMethod5() {
        // 方法5: 使用动态Feign客户端访问
        String result = dynamicStockFeignClient.getStock();
        System.out.println("动态Feign客户端访问结果: " + result);
        return result;
    }

    /**
     * 创建订单
     */
    @PostMapping
    public String createOrder(@RequestBody Order order) {
        int rows = orderService.createOrder(order);
        return rows > 0 ? "订单创建成功，订单号：" + order.getOrderNo() : "创建失败";
    }

    /**
     * 查询用户的所有订单
     */
    @GetMapping("/user/{userId}")
    public List<Order> getOrdersByUserId(@PathVariable Long userId) {
        return orderService.getOrdersByUserId(userId);
    }

    /**
     * 根据订单号查询订单
     */
    @GetMapping("/{orderNo}")
    public Order getOrderByNo(@PathVariable String orderNo) {
        return orderService.getOrderByNo(orderNo);
    }
}
